from sqlmodel import SQLModel, Session, select

import models
from crud.get_user_information import userCrud
from crud.add_logger import logger
from core import get_password_hash
from db import engine
from models import *
from typing import Optional
from crud.base import CRUDBase
from db.session import get_session
# from sqlalchemy.orm import Session
from utils.table_error import ProvinceNameNotExist
from models.user import *
from schemas import provincecity_Pydantic
# 只对city这个表进行增删改查操作
# 实现逻辑 先对省份表进行编辑


def get_id_provincial(provincial_id: int, db: Session = get_session()):
    """ 通过id确定省份 """
    sql = select(models.Provincial).where(models.Provincial.id == provincial_id)
    result = db.scalar(sql).first()
    db.close()
    return result

def IfExistProvince(province_name: str, db: Session = get_session()):
    """ 通过名字判断省份是否存在数据库中 """
    sql = select(models.Provincial).where(models.Provincial.name == province_name)
    result = db.scalar(sql).first()
    if not result:
        raise ProvinceNameNotExist("查询省份名称不存在")
    return result

def CreateProvince(newProvince: provincecity_Pydantic.ProvincialCreate, db: Session = get_session()):
    """ 创建新的省份信息 """
    db_newprovincename = models.Provincial(name = newProvince)
    db.add(db_newprovincename)
    db.commit()
    db.refresh(db_newprovincename)
    return db_newprovincename

def Getprovince(db: Session = get_session()):
    sql = select(models.Provincial)
    result = db.scalar(sql)
    return result

















